/**
 *
 *
 * @flow
 */

import React, {Component} from 'react';
import {
    AppRegistry,
    StyleSheet,
    Text,
    View,
    Image,
    ListView,
    TouchableOpacity,
    RefreshControl,
    InteractionManager,
    ScrollView
} from 'react-native';
import Header from '../Home/Header';
import GCStyle from '../../Model/Global/GCStyle';
import GCComponent from '../../Model/Global/GCComponent';


import {connect} from "react-redux";
import {GetHomeInfo, GetCarList} from "../../Action/UserAction";
import Toast from "react-native-toast";
class OrderList extends Component {
    constructor(props) {
        super(props);
        var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
        this.state = {
            dataSource: ds,
            selectRow: 0,
            date_type: 'today',
        };
    }

    componentDidMount() {
        InteractionManager.runAfterInteractions(() => {
            const {dispatch} = this.props;
            dispatch({type: 'Complete', D_CarList_IsRefreshing: true});
            dispatch(GetHomeInfo(this.state.date_type));
            dispatch(GetCarList(1));
        });
    }

    componentWillUpdate(nextProps, nextState) {
        InteractionManager.runAfterInteractions(() => {
            const {UserReducer, dispatch} = nextProps;
            if (UserReducer.D_Home_IsRefreshing) {
                dispatch({type: 'Complete', D_Home_IsRefreshing: false});
                dispatch(GetCarList(1));
            }
        });
    }


    onClickRow(rowID) {
    }

    onSetlectAction(date_type) {
        this.setState({
            date_type: date_type,
        });
        InteractionManager.runAfterInteractions(() => {
            const {dispatch} = this.props;
            dispatch(GetHomeInfo(date_type));
        });
    }

    rowView(data, sectionID, rowID) {
        return (
            <TouchableOpacity activeOpacity={0.8} onPress={() => this.onClickRow(rowID)} style={styles.orderRowView}>
                <View style={{
                    height: 50,
                    justifyContent: 'space-between',
                    width: GCStyle.GCPubWidth,
                    alignItems: 'center',
                    flexDirection: 'row'
                }}>
                    <Text style={[GCStyle.GCOrderCell.conterTxt, {marginLeft: 15}]}>{data.bus_card}</Text>
                    <Image source={require('../../Resource/Imgs/app_icon_more.png')}
                           style={{marginRight: 15, width: 6, height: 11}}/>
                    <View style={[GCStyle.GCOrderCell.lineStyle, {marginTop: 47}]}/>
                </View>
            </TouchableOpacity>
        );
    }

    _renderSectionHeader() {
        return (
            <View style={{height: 10}}/>
        );
    }

    cellHeader() {
        const {UserReducer} = this.props;
        var shop_id = '';
        if (UserReducer.shop_id === 0) {
            shop_id = '自营';
        } else {
            shop_id = '非自营';
        }
        return (
            this.props.isShowHeader == true ? <Header head_data={UserReducer.D_HomeInfo}
                                                      partners={shop_id}
                                                      onSetlectAction={(name) => this.onSetlectAction(name)}/> : null
        );
    }

    render() {
        const {UserReducer} = this.props;
        var shop_id = '';
        if (UserReducer.shop_id === 0) {
            shop_id = '自营';
        } else {
            shop_id = '非自营';
        }
        return (
            <View style={styles.container}>
                <ScrollView
                    refreshControl={
                        <RefreshControl
                            refreshing={UserReducer.D_CarList_IsRefreshing}
                            onRefresh={() => this.refreshData()}
                        />
                    }
                >
                <Header head_data={UserReducer.D_HomeInfo}
                        partners={shop_id}
                        onSetlectAction={(name) => this.onSetlectAction(name)}/>
                    {this.load()}
                </ScrollView>
            </View>
        );
    }
    load() {
        const {UserReducer} = this.props;
        if (UserReducer.D_CarList == null || UserReducer.D_CarList == '') {
            return (
                <ScrollView showsVerticalScrollIndicator={false}>
                    <View
                        style={{alignItems: 'center', marginTop: 30}}>
                        <Image resizeMode='contain'
                               source={require('../../Resource/Imgs/results_image_error_order.png')}
                               style={[GCStyle.Style.loginIcon]}/>
                        <Text style={{color: '#bababa'}}>{this.loadViewText()}</Text>
                    </View>
                </ScrollView>
            );
        } else {
            return <ListView
                style={{flex: 1}}
                scrollEnabled={false}
                dataSource={this.state.dataSource.cloneWithRows(UserReducer.D_CarList)}
                enableEmptySections={true}
                renderSectionHeader={() => this._renderSectionHeader()}
                renderRow={(data, sectionID, rowID) => this.rowView(data, sectionID, rowID)}
                onEndReachedThreshold={50}
                onEndReached={() => {
                    this.loadMoreData()
                }}
            />;
        }
    }
    loadViewText() {
        const {UserReducer} = this.props;
        if (UserReducer.D_CarList == null || UserReducer.D_CarList == '') {
            if (UserReducer.D_CarList_IsRefreshing) {
                return '正在加载';
            } else {
                return '暂无数据';
            }
        }
    }

    refreshData() {
        const {dispatch} = this.props;
        dispatch({type: 'Complete', D_CarList_IsRefreshing: true});
        dispatch(GetCarList(1));
    }

    loadMoreData() {
        const {UserReducer, dispatch} = this.props;
        let inNext = UserReducer.D_CarList_InNext;
        let page = UserReducer.D_CarList_Page;

        if (inNext !== 0) {
            InteractionManager.runAfterInteractions(() => {
                dispatch(GetCarList(Number(page) + 1));
            });
        } else {
            if (Number(page) !== 1) {
                Toast.show('木有更多数据');
            }
        }
    }

}
const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
    },
    icoBn: {
        height: 150,
        width: 50,
        borderRadius: 25,
        marginRight: 3,
    },
    sectionRow: {
        backgroundColor: 'red'
    },
    sectionRowTxt: {
        fontSize: 14,
        color: '#080808',
        textAlign: 'center',
    },
    orderRowView: {
        width: GCStyle.GCPubWidth,
        backgroundColor: 'white',
    },
});


export default connect((state) => {
    return {UserReducer} = state;
})(OrderList);

